package main.java.com.negromotad.view;

import java.awt.ComponentOrientation;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import main.java.com.negromotad.configuration.TMGeneticAlgorithmConfigurator;
import main.java.com.negromotad.controller.DefaultController;

public class PopUpView extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4796763663163729114L;
	private JPanel contentPane;
	private JTextField maxAllowedEvolution;
	private JTextField populationSize;
	private JTextField maxFailureEvolutions;
	private JTextField sameResultEvolution;
	private DefaultController controller;
	private JCheckBox chckbxPreserveFirstIndividual;
	private JCheckBox chckbxMinimize;
	private TMGeneticAlgorithmConfigurator tmConfig;

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame.
	 */
	public PopUpView(DefaultController controller) {
		tmConfig = new TMGeneticAlgorithmConfigurator();
		this.controller = controller;
		controller.setAlgorithmConfiguration(tmConfig);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 298, 318);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "Configuration",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		chckbxPreserveFirstIndividual = new JCheckBox(
				"Preserve First Individual");
		chckbxPreserveFirstIndividual.setSelected(true);
		chckbxPreserveFirstIndividual
				.setToolTipText("Preserve the first chromosome for use in the evolution");
		chckbxPreserveFirstIndividual
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		chckbxPreserveFirstIndividual.setBounds(6, 57, 146, 23);
		contentPane.add(chckbxPreserveFirstIndividual);

		chckbxMinimize = new JCheckBox("Minimize");
		chckbxMinimize.setToolTipText("Use minimize fitness function");
		chckbxMinimize
				.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		chckbxMinimize.setBounds(6, 31, 97, 23);
		contentPane.add(chckbxMinimize);

		maxAllowedEvolution = new JTextField();
		maxAllowedEvolution.setBounds(143, 101, 86, 20);
		maxAllowedEvolution.setText("1000");
		contentPane.add(maxAllowedEvolution);
		maxAllowedEvolution.setColumns(10);

		populationSize = new JTextField();
		populationSize.setBounds(143, 129, 86, 20);
		populationSize.setText("200");
		contentPane.add(populationSize);
		populationSize.setColumns(10);

		maxFailureEvolutions = new JTextField();
		maxFailureEvolutions.setBounds(143, 160, 86, 20);
		maxFailureEvolutions.setText("150");
		contentPane.add(maxFailureEvolutions);
		maxFailureEvolutions.setColumns(10);

		sameResultEvolution = new JTextField();
		sameResultEvolution.setBounds(143, 191, 86, 20);
		sameResultEvolution.setText("250");
		contentPane.add(sameResultEvolution);
		sameResultEvolution.setColumns(10);

		JLabel lblMaxallowedevolution = new JLabel("Max Allowed Evolution");
		lblMaxallowedevolution
				.setToolTipText(" Maximum evolutions alowed whitout results, before stop");
		lblMaxallowedevolution.setBounds(10, 101, 123, 14);
		contentPane.add(lblMaxallowedevolution);

		JLabel lblNewLabel = new JLabel("Population Size");
		lblNewLabel
				.setToolTipText("Maximum number of results managed by the algorithm");
		lblNewLabel.setBounds(10, 132, 97, 14);
		contentPane.add(lblNewLabel);

		JLabel lblMaxfailureevolutions = new JLabel("Max Failure Evolutions");
		lblMaxfailureevolutions
				.setToolTipText("Maximum evolutions alowed whit out results, before stop");
		lblMaxfailureevolutions.setBounds(10, 166, 119, 14);
		contentPane.add(lblMaxfailureevolutions);

		JLabel lblSameresultevolutions = new JLabel("Same Result Evolutions");
		lblSameresultevolutions
				.setToolTipText("Maximum number of equals results before stop");
		lblSameresultevolutions.setBounds(10, 197, 119, 14);
		contentPane.add(lblSameresultevolutions);

		JButton btnApply = new JButton("Apply");
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!validateElements()) {
					JOptionPane.showMessageDialog(new JFrame(),
							"Invalid entry type values. Must be integer type",
							"Error", JOptionPane.ERROR_MESSAGE);

				} else
					setConfguration(arg0);
			}

		});
		btnApply.setBounds(96, 246, 89, 23);
		contentPane.add(btnApply);
	}

	private boolean validateElements() {
		boolean isInteger = Pattern.matches("\\d*", maxAllowedEvolution
				.getText());
		if (!isInteger)
			return false;
		boolean isInteger1 = Pattern.matches("\\d*", populationSize.getText());
		if (!isInteger1)
			return false;
		boolean isInteger2 = Pattern.matches("\\d*", maxFailureEvolutions
				.getText());
		if (!isInteger2)
			return false;
		boolean isInteger3 = Pattern.matches("\\d*", sameResultEvolution
				.getText());
		if (!isInteger3)
			return false;
		return true;

	}

	private void setConfguration(ActionEvent arg0) {

		tmConfig.setPreserveFirstIndividual(chckbxPreserveFirstIndividual
				.isSelected());
		tmConfig.setMaxAllowedEvolution(Integer.parseInt(maxAllowedEvolution
				.getText()));
		tmConfig.setPopulationSize(Integer.parseInt(populationSize.getText()));
		tmConfig.setMinimize(chckbxMinimize.isSelected());
		tmConfig.setMaxFailureEvolutions(Integer.parseInt(maxFailureEvolutions
				.getText()));
		tmConfig.setSameResultEvolutions(Integer.parseInt(sameResultEvolution
				.getText()));
		controller.setAlgorithmConfiguration(tmConfig);
		this.setVisible(false);

	}

	public int getMaximumProgressBar() {
		return tmConfig.getMaxAllowedEvolution();
	}

}
